package com.swifthealth.controller;

import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * db demo Controller
 *
 * @author houxiurong
 * @date 2021-03-16
 */
@Slf4j
@RestController
public class DbReportController {

    @Resource
    private DataSource dataSource;

    /**
     * 转换为pdf展示
     *
     * @param reportName
     * @param response
     * @throws SQLException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/demo/{reportName}")
    public void getDbReportByParam(@PathVariable("reportName") final String reportName, HttpServletResponse response)
            throws SQLException, JRException, IOException {
        log.info("reportName={}", reportName);
        String jrXmlPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/" + reportName + ".jrxml";
        String jasperPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/" + reportName + ".jasper";

        JasperCompileManager.compileReportToFile(jrXmlPath, jasperPath);

        Map parameters = new HashMap<String, String>();
        parameters.put("COMPANY", "朝前科技");

        // 获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + reportName + ".jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        IOUtils.closeQuietly(jasperStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 产品统计
     *
     * @param response
     * @throws SQLException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/product/sum")
    public void getDbReportProduct(@RequestParam("whereSql") String whereSql, HttpServletResponse response) throws SQLException, JRException, IOException {
        String jrXmlPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/product.jrxml";
        String jasperPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/product.jasper";

        JasperCompileManager.compileReportToFile(jrXmlPath, jasperPath);

        Map parameters = new HashMap<String, String>();
        parameters.put("whereSql", whereSql);

        // 获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + "product.jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        IOUtils.closeQuietly(jasperStream);
        IOUtils.closeQuietly(outputStream);
    }


    /**
     * 产品统计
     *
     * @param response
     * @throws SQLException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/coffee")
    public void getDbReportCoffee(HttpServletResponse response) throws SQLException, JRException, IOException {
        String jrXmlPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/coffee.jrxml";
        String jasperPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/coffee.jasper";

        JasperCompileManager.compileReportToFile(jrXmlPath, jasperPath);

        Map parameters = new HashMap<String, String>();
        //parameters.put("whereSql", whereSql);

        // 获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + "product.jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        IOUtils.closeQuietly(jasperStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 产品统计
     *
     * @param response
     * @throws SQLException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/shengou")
    public void getDbReportShengou(@RequestParam("id") Integer id, HttpServletResponse response) throws SQLException, JRException, IOException {
        String jrXmlPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/shengou.jrxml";
        String jasperPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/shengou.jasper";

        JasperCompileManager.compileReportToFile(jrXmlPath, jasperPath);

        Map parameters = new HashMap<String, String>();
        parameters.put("id", id);

        // 获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + "shengou.jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        IOUtils.closeQuietly(jasperStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 产品统计
     *
     * @param response
     * @throws SQLException
     * @throws JRException
     * @throws IOException
     */
    @GetMapping("/chart1")
    public void getDbReportChart1(HttpServletResponse response) throws SQLException, JRException, IOException {
        String jrXmlPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/chart1.jrxml";
        String jasperPath = "/home/houxiurong/gitee/report-spring-boot/src/main/resources/jaspers/chart1.jasper";

        JasperCompileManager.compileReportToFile(jrXmlPath, jasperPath);

        Map parameters = new HashMap<String, String>();
        //parameters.put("id", id);

        // 获取文件流
        ClassPathResource resource = new ClassPathResource("jaspers" + File.separator + "chart1.jasper");
        InputStream jasperStream = resource.getInputStream();

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline;");
        final OutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        IOUtils.closeQuietly(jasperStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 上传文件
     * @param request
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public String uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        try {
            if (!file.isEmpty()) {
                String realPath = request.getServletContext().getRealPath("/reports");
                String filename = file.getOriginalFilename();
                File newFile = new File(realPath, filename);
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                file.transferTo(newFile);
                return filename;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "file upload error";
        }
        return "file upload ok";
    }

}
